/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tcloudclient.actionManager;

import api.action.ActionInterface;
import api.action.ActionPackageClient;
import java.util.ArrayList;
import java.util.List;
import tcloudclient.contex.Context;

/**
 *
 * @author Tomas
 */
public class ActionExecutor {

    private List<ExecuteActionInterface> actionExecutors;
    private List<ActionExecuteEventInterface> handlers;

    public ActionExecutor() {
        actionExecutors = new ArrayList<>();
        handlers = new ArrayList<>();
        initExecutors();
    }

    private void initExecutors() {
        actionExecutors.add(new NewFileActionExecutor());
        actionExecutors.add(new NewFolderActionExecutor());
        actionExecutors.add(new UpdateFileActionExecutor());
        actionExecutors.add(new UpdateFolderActionExecutor());
        actionExecutors.add(new DeleteActionExecutor());
    }

    public void excuteActions(ActionPackageClient actionPackage) {

        executeActions(actionPackage.getActions());
        Context.getInstance().getApplicationSettings().setAccHeaderVetsion(actionPackage.getAccountHeaderVersion());
    }

    private void executeActions(List<ActionInterface> actions) {
        for (ActionInterface action : actions) {
            findAndExecuteAction(action);
        }
    }

    private void findAndExecuteAction(ActionInterface action) {
        for (ExecuteActionInterface executeAction : actionExecutors) {
            if (executeAction.isSupported(action.getActionType())) {
                executeAction.execute(action.getActionData(), new FireActionExecuteInterface() {

                    @Override
                    public boolean canActionExecute(String[] path, long version, ActionTypeClientEnum actionType) {
                        return fireActionCanExecuteEvent(path, version, actionType);
                    }

                    @Override
                    public void actionExecuted(String[] path, long version, ActionTypeClientEnum type) {
                        fireActionExecutedEvent(path, version, type);
                    }
                });
                break;
            }
        }

    }

    public void addActionListner(ActionExecuteEventInterface listener) {
        handlers.add(listener);
    }

    private boolean fireActionCanExecuteEvent(String[] path, long version, ActionTypeClientEnum actionType) {
        boolean result = true;
        for (ActionExecuteEventInterface actionExecuteEventInterface : handlers) {
            result = actionExecuteEventInterface.canExecuteAction(path, version, actionType);
            if (!result) {
                break;
            }
        }
        return result;
    }

    private void fireActionExecutedEvent(String[] path, long version, ActionTypeClientEnum actionType) {
        for (ActionExecuteEventInterface actionExecuteEventInterface : handlers) {
            actionExecuteEventInterface.actionExecuted(path, version, actionType);
        }
    }
}
